﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OrganizationName.Factory.LifeTimeContracts
{
    public class ContainerLifeTimeContract : ILifeTimeContract
    {
        private bool _InstanceSet;
        private object _Instance;

        public bool IsValid
        {
            get { return _InstanceSet; }
        }

        public object Instance
        {
            get
            {
                return _Instance;
            }
            set
            {
                lock (this)
                {
                    if (_InstanceSet)
                    {
                        throw new InvalidOperationException("Because of the behaviour of this life time contract, the instance cannot be overwritten once set.");
                    }
                    _Instance = value;
                    _InstanceSet = true;
                }
            }
        }


        public object SyncRoot
        {
            get
            {
                return this;
            }
        }
    }
}
